<?php

namespace app\admin\service;

use Couchbase\ValueRecorder;

/**
 * QuestionForm
 * @internal
 */
class QuestionForm
{
    /**
     * action
     * @var int
     */
    protected $action;

    /**
     * questionType
     * @var int
     */
    protected $questionType;

    /**
     * questionData
     * @var array
     */
    protected $questionData;

    /**
     * selectOptions
     * @var string[]
     */
    protected $selectOptions = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];

    /**
     * questionTypeMap
     * @var string[]
     */
    protected $questionTypeMap = [
        '1' => '单选',
        '2' => '多选',
        '3' => '判断',
        '4' => '问答',
        '5' => '填空',
        '6' => '完形填空',
        '7' => '阅读理解',
        '8' => '名词解释',
    ];

    public function getQuestionTypeMap(): array
    {
        return $this->questionTypeMap;
    }

    public function setQuestionTypeMap(array $questionTypeMap): void
    {
        $this->questionTypeMap = $questionTypeMap;
    }

    /**
     * trueOrFalseOptions
     * @var string[]
     */
    protected $trueOrFalseOptions = [
        'A' => '正确',
        'B' => '错误',
    ];

    /**
     * __construct
     * @param $action
     * @param $questionType
     * @param $questionData
     */
    public function __construct($action, $questionType, $questionData)
    {
        $this->action = $action;
        $this->questionType = $questionType;
        $this->questionData = $questionData;
    }

    /**
     * build
     * @return string
     */
    public function build(): string
    {
        $s = $this->buildType();
        $s .= $this->buildName();
        $s .= $this->buildSelect();
        $s .= $this->buildTrueOrFalse();
        $s .= $this->buildCorrectOption();
        $s .= $this->buildExplanation();
        return $s;
    }

    /**
     * buildType
     * @return string
     */
    private function buildType(): string
    {
        $dom = build_select(
            'row[question_type]',   // 表单name名称
            [
                '1' => '单选',
                '2' => '多选',
                '3' => '判断',
                '4' => '问答',
                '5' => '填空',
                '6' => '完形填空',
                '7' => '阅读理解',
                '8' => '名词解释',
            ],
            [$this->questionType ?: 1],  // 默认选中值
            ['class' => 'form-control selectpicker']
        );
        return <<<EOT
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">题目类型:</label>
        <div class="col-xs-12 col-sm-8">
           $dom
        </div>
    </div>
EOT;
    }

    /**
     * buildName
     * @return string
     */
    private function buildName(): string
    {
        $value = $this->questionData['questionAsk'] ?? '';
        $class = 'editor';
        return <<<EOT
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">题目标题:</label>
        <div class="col-xs-12 col-sm-8">
            <textarea id="c-questionAsk" data-height="50" data-min-height="50" data-rule="required" class="form-control $class" name="row[questionAsk]">$value</textarea>
        </div>
    </div>
EOT;
    }

    private function buildSelect(): string
    {
        if (!in_array($this->questionType, [1, 2])) {
            return '';
        }
        $class = 'editor';
        $template = <<<EOT
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">%s:</label>
        <div class="col-xs-12 col-sm-8">
            <textarea id="c-%s" data-height="50" data-min-height="50" class="form-control $class" name="row[%s]">%s</textarea>
        </div>
    </div>
EOT;
        if ($this->action == 1) {
            $s = '';
            foreach ($this->selectOptions as $option) {
                $s1 = $s2 = $s3 = $s4 = $option;
                $s1 = '选项' . $s1;
                $s .= sprintf($template, $s1, $s2, $s3, $s4);
            }
        } else {
            $selectOptions = $this->questionData['answerOption'] ?? [];
            $s = '';
            foreach ($this->selectOptions as $key => $option) {
                $s1 = $s2 = $s3 = $option;
                $s1 = '选项' . $s1;
                if (!empty($selectOptions[$option])) {
                    $s4 = $selectOptions[$option];
                } else {
                    $s4 = '';
                }
                $s .= sprintf($template, $s1, $s2, $s3, $s4);
            }
        }
        return $s;
    }

    private function buildTrueOrFalse(): string
    {
        if ($this->questionType != 3) {
            return '';
        }
        $template = <<<EOT
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">%s:</label>
        <div class="col-xs-12 col-sm-8">
            <input id="c-%s"  readonly class="form-control" name="row[%s]" type="text" value="%s">
        </div>
    </div>
EOT;
        $s = '';
        foreach ($this->trueOrFalseOptions as $key => $val) {
            $s1 = '选项' . $key;
            $s .= sprintf($template, $s1, $key, $key, $val);
        }
        return $s;
    }

    /**
     * buildCorrectOption
     * @return string
     */
    private function buildCorrectOption(): string
    {
        $value = $this->questionData['correctOption'] ?? '';
        if ($this->questionType < 4) {
            $label = '正确选项';
            $selected = $value;
            if (!$selected) {
                $selected = $this->selectOptions[0];
            }

            // 非多选
            if ($this->questionType != 2) {
                $inputDom = build_select(
                    'row[correctOption]',   // 表单name名称
                    $this->buildCorrectOptionSelect($this->questionType),
                    [$selected],  // 默认选中值
                    ['class' => 'form-control selectpicker']
                );
            } else {
                // 多选处理
                $selected = str_split($selected);
                $inputDom = build_checkboxs(
                    'row[correctOption][]',   // 表单name名称
                    $this->buildCorrectOptionSelect($this->questionType),
                    $selected,
                );
            }

        } else {
            $label = '题目答案';
            $inputDom = '<textarea id="c-correctOption" data-height="50" data-min-height="50" data-rule="required" class="form-control editor" name="row[correctOption]">' . $value . '</textarea>';
        }
        return <<<EOT
            <div class="form-group">
                <label class="control-label col-xs-12 col-sm-2">$label:</label>
                <div class="col-xs-12 col-sm-8">
                    $inputDom
                </div>
            </div>
EOT;
    }

    private function buildCorrectOptionSelect($questionType)
    {
        // 提取选项
        $selectOptions = $this->selectOptions;

        // 判断题只需要AB
        if ($questionType == 3) {
            $selectOptions = array_slice($selectOptions, 0, 2);
        }
        $select = [];
        foreach ($selectOptions as $val) {
            $select[$val] = $val;
        }
        return $select;
    }

    /**
     * buildExplanation
     * @return string
     */
    private function buildExplanation(): string
    {
        $value = $this->questionData['explanation'] ?? '';
        $class = 'editor';
        return <<<EOT
    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">题目解析:</label>
        <div class="col-xs-12 col-sm-8">
            <textarea id="c-explanation" data-height="50" data-min-height="50" class="form-control $class" name="row[explanation]">$value</textarea> 
        </div>
    </div>
EOT;
    }
}
